package com.example.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.Communication;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.util.CallBack;
import com.example.util.Util;

public class WelcomeActivity extends Activity {
	private Thread thread;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_welcome);
		startMainService();
		checkNet();
	}

	private void startMainService() {
		Intent mainService = new Intent(this, MainService.class);
		startService(mainService);
	}

	private void checkNet() {
		String net = MainService.setNetworkType(this);
		if ("".equals(net)) {
			new AlertDialog.Builder(this)
			.setTitle("当前没有可用的网络")
			.setMessage("请开启网络")
			.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int which) {
							finish();
							System.exit(0);
						}
					}).show();
		} else if("wifi".equals(net)){  
			start();
			return;
		}else{
			AlertDialog alert= new AlertDialog.Builder(this).create();
			alert.setTitle("网络连接使用的是流量数据");
			alert.setMessage("建议开启wifi");
			alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					start();
				}
			});
			alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					System.exit(0);
				}
			});
			alert.show();
		}
		return;

	}
	private String data;
	private String url;
	private void start() {
		thread = new Thread() {
			private SharedPreferences sp;
			private Editor spEditor;

			public void run() {
				 data=NetRequest.getIndexImg();
							if(!data.equals("")){
								try {
									JSONObject j=new JSONObject(data);
									String result=j.getString("pic");
									if(!result.equals("")){
										url=NetRequest.requesthttp+result;
										HSApplication.url=url;
										sp=HSApplication.sp;
										spEditor=sp.edit();
										spEditor.putString("backUrl", url);
										spEditor.commit();
									}else{
										url="";
									}
									Thread.sleep(3500);
									h.sendEmptyMessage(1);
									return;
								}catch (JSONException e) {
									h.sendEmptyMessage(2);
									e.printStackTrace();
									return;
								} catch (InterruptedException e) {
									h.sendEmptyMessage(2);
									e.printStackTrace();
									return;
								}
							}else{
								h.sendEmptyMessage(2);
								return;
							}
		}
		};
		thread.start();
	}
	public void isAccountTimeOut(){
		
	}
	public  Handler h=new Handler(){
		public void handleMessage(Message msg) {
			Boolean isAutoLogin=HSApplication.sp.getBoolean("isAutoLogin", false);
			String username=HSApplication.sp.getString("mobile","");
			String pwd=HSApplication.sp.getString("pwd", "");
			switch (msg.what) {
			case 1:
				if (isAutoLogin == true&&!"".equals(username)&&!"".equals(pwd)) {
					Communication.login(username, pwd,new CallBack() {
						@Override
						public void run(Boolean isError, Object data) {
							if(isError){
								if("true".equals(data)){
									 SharedPreferences sp=HSApplication.sp;
									 Editor spEditor=sp.edit();
									Intent i = new Intent();
									i.setClass(WelcomeActivity.this, MainActivity.class);
									startActivity(i);
									IndexActivity.isLogin = true;
									spEditor.putBoolean("isAutoLogin", true);
									spEditor.commit();
									finish();
									return;
								}else {
											Intent intent=new Intent(WelcomeActivity.this,
													LoginActivity.class);
											startActivity(intent);
											finish();
									return;
								}
							}
						}
					});
				} else {
					Intent intent=new Intent(WelcomeActivity.this,
							IndexActivity.class);
					startActivity(intent);
				}
				finish();
				break;
			case 2:
				if (MainService.isAutoLogin == true) {
					startActivity(new Intent(WelcomeActivity.this,
							MainActivity.class));
				} else {
					Intent intent=new Intent(WelcomeActivity.this,
							IndexActivity.class);
					startActivity(intent);
				}
				finish();
				break;
			}
		}
	};

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			try {
				if (thread.isAlive()) {
					thread.stop();
				}

				finish();
				System.exit(0);

			} catch (Exception e) {
				System.exit(0);
			}

		}
		return super.onKeyDown(keyCode, event);
	}

}
